// Demonstration of zlib utility functions
//   http://www.codeguru.com/cpp/cpp/algorithms/compression/article.php/c11735/

#include <stdio.h>
#include <zlib.h>
#include <sys/types.h>			// para lseek()
#include <unistd.h>

#include "zlib_use.h"

unsigned long 
file_size(char *filename)
{
	FILE *pFile = fopen(filename, "rb");

	fseek (pFile, 0, SEEK_END);
	unsigned long size = ftell(pFile);
	fclose (pFile);

	return size;
}


int 
decompress_one_file(char *infilename, char *outfilename)
{
	char buff[128];
	int num_read = 0;
    	//int err;

	gzFile infile = gzopen(infilename, "rb");
	FILE *outfile = fopen(outfilename, "wb");

	if (!infile || !outfile)
		return -1;

	while ((num_read = gzread(infile, buff, sizeof(buff))) > 0) 
	{
 		fwrite(buff, 1, num_read, outfile);
 	}

 	gzclose(infile);
	fclose(outfile);
    
	return 1;
}


int 
compress_one_file(char *infilename, char *outfilename)
{
	char inbuffer[128];
	int num_read;

	FILE *infile = fopen(infilename, "rb");
	gzFile outfile = gzopen(outfilename, "wb");

	if (!infile || !outfile)
		return -1;

	while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) 
	{
		gzwrite(outfile, inbuffer, num_read);
	}

	fclose(infile);
	gzclose(outfile);

	return 1;
}

